package cn.gztang.filter;

import cn.gztang.constant.ErrorCode;
import cn.gztang.constant.UserType;
import cn.gztang.entity.dto.ResultBean;
import cn.gztang.entity.vo.UserInfoVO;
import cn.gztang.util.ValidateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author GaoFeng2017
 * @date 2018/9/9 20:42
 */
public class AdminAccessInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取sesion
        HttpSession session = request.getSession();

        // 获取用户信息
        UserInfoVO infoVO = (UserInfoVO) session.getAttribute(UserType.LOGIN_INFO);

        // 校检用户
        if (ValidateUtil.isNull(infoVO)) {
            response.getWriter().write(JSON.toJSONString(new ResultBean<>(false,ErrorCode.NOT_LOGGED_ON,"用户未登录")));
            return false;
        }
        
        return true;
    }
}
